//
//  MSBankWithdrawalController.m
//  觅尚
//
//  Created by No.8 on 2019/4/1.
//  Copyright © 2019 No.8. All rights reserved.
//

#import "MSBankWithdrawalController.h"
#import "MSBuidingBankCardController.h"
#import "MSBankCardListController.h"
#import "MSBackCardModel.h"

@interface MSBankWithdrawalController ()<UITableViewDelegate,UITableViewDataSource,MSBankCardListDelegate>
{
    CustomNavigationBar *naviagtionBar;
}


/** 列表*/
@property(nonatomic,strong)MSBankCardListController * BankCardListVC;

/** 表视图*/
@property(nonatomic,strong)UITableView *tableView;


/** 遮盖层*/
@property(nonatomic,strong)UIView *backView;


/** 弹出View*/
@property(nonatomic,strong)UIView *showView;


/** 银行昵称*/
@property(nonatomic,strong)UILabel * ChoiceBank;


/** 持卡人昵称*/
@property(nonatomic,strong)UILabel * ChoiceBankName;


/** 数组*/
@property(nonatomic,strong)NSMutableArray *bankcardList;

/** 可用余额*/
@property(nonatomic,strong)UILabel *AvailablebalanceLabel;


/** 输入框*/
@property(nonatomic,strong)UITextField *CashAmountTextField;

/** 银行卡id*/
@property(nonatomic,strong)NSString *banid;

/** 可用余额*/
@property(nonatomic,strong)NSString *AccountMoney;



@end

@implementation MSBankWithdrawalController

- (int)navBarBottom {
    if ([WRNavigationBar isIphoneX]) {
        return 88;
    } else {
        return 64;
    }
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    self.navigationController.navigationBar.hidden = YES;
    self.bankcardList = [NSMutableArray new];
    dispatch_async(dispatch_get_main_queue(), ^{
        
        [self ReloadData];
        
    });
}
- (void)ReloadData{
    
    NSDictionary * params = @{@"uid":[WYCAccountTool unarchiveuid]};
    [HTTPRequest POST:MSgetRecord params:params succeed:^(id responseObject) {
        
        WYCLog(@"--------%@",responseObject);
        if (CodeStr) {
            self.AvailablebalanceLabel.text = [NSString stringWithFormat:@"可用余额%@元",responseObject[@"data"][@"AccountMoney"]];
            self.AccountMoney = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"AccountMoney"]];
        }else{
            
            [MBManager showBriefAlert:responseObject[@"msg"]];
            
        }
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [MBManager showBriefAlert:Failed];
    }];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:YES];
    self.navigationController.navigationBar.hidden = YES;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self CreateNavigationBar];
    
    //    self.view.backgroundColor = backgroundcolor;
    
    [self CreatView];
    
    
}

- (void)CreatView{
    UIView * BankView = [[UIView alloc] initWithFrame:CGRectMake(30 * px, [self navBarBottom], ScreenWidth - 60 * px, 300 * px)];
    BankView.backgroundColor = backgroundcolor;
    
    CAGradientLayer *gradientLayer =  [CAGradientLayer layer];
    gradientLayer.frame = BankView.bounds;
    gradientLayer.cornerRadius = 30 * px;
    gradientLayer.startPoint = CGPointMake(0, 0);
    //        gradientLayer
    gradientLayer.endPoint = CGPointMake(1, 0);
    gradientLayer.locations = @[@(0.2),@(1.0)];//渐变点
    [gradientLayer setColors:@[(id)[WYCRGBColor(96, 59, 152) CGColor],(id)[WYCRGBColor(202, 72, 114) CGColor]]];//渐变数组
    [BankView.layer addSublayer:gradientLayer];
    [self.view addSubview:BankView];
    
    
    
    /** 日历按钮*/
    UIImageView * bankIMG = [[UIImageView alloc] initWithFrame:CGRectMake(30 * px, BankView.height / 2 - 60 * px, 120 * px, 120 * px)];
    bankIMG.image = [UIImage imageNamed:@""];
    bankIMG.layer.cornerRadius = 60 * px;
    bankIMG.backgroundColor = BackViewColor;
    [BankView addSubview:bankIMG];
    
    
    
    /** 银行*/
    UILabel * ChoiceBank = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(bankIMG.frame) + 30 * px,bankIMG.y,BankView.width * 0.6, 60 * px)];
    ChoiceBank.textColor = [UIColor whiteColor];
    ChoiceBank.text = @"请选择银行";
//    ChoiceBank.textAlignment = <#name#>
    ChoiceBank.font = [UIFont boldSystemFontOfSize:SYRealValue(35 / 2)];
    [BankView addSubview:ChoiceBank];
    self.ChoiceBank = ChoiceBank;
    
    
    /** 银行*/
    UILabel * ChoiceBankName = [[UILabel alloc] initWithFrame:CGRectMake(ChoiceBank.x,CGRectGetMaxY(ChoiceBank.frame) + 20 * px,ChoiceBank.width, ChoiceBank.height)];
    ChoiceBankName.textColor = ChoiceBank.textColor;
    ChoiceBankName.text = @"";
    //    ChoiceBank.textAlignment = <#name#>
    ChoiceBankName.font = [UIFont boldSystemFontOfSize:SYRealValue(28 / 2)];
    [BankView addSubview:ChoiceBankName];
    self.ChoiceBankName = ChoiceBankName;
    
    /** 银行选择按钮*/
    UIButton * ChoiceBankButton = [[UIButton alloc] initWithFrame:CGRectMake(BankView.width - 30 - 10 * px, BankView.height / 2 - 15, 30, 30)];
    [ChoiceBankButton  setImage:[UIImage imageNamed:@"txarrow"] forState:UIControlStateNormal];
    [ChoiceBankButton  addTarget:self action:@selector(ChoiceBankButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [BankView  addSubview:ChoiceBankButton];
    
    
    
    
    UIView * BankWithdrawalView = [[UIView alloc] initWithFrame:CGRectMake(BankView.x, CGRectGetMaxY(BankView.frame) + 50 * px, BankView.width, 450 * px)];
    BankWithdrawalView.backgroundColor = [UIColor clearColor];
    BankWithdrawalView.layer.cornerRadius = 10 * px;
    BankWithdrawalView.layer.masksToBounds = YES;
    //给图层添加一个有色边框
    BankWithdrawalView.layer.borderWidth = 1;
    BankWithdrawalView.layer.borderColor = BackViewColor.CGColor;
    [self.view addSubview:BankWithdrawalView];
    
    
    /** 提现金额*/
    UILabel * CashAmountTitle = [[UILabel alloc] initWithFrame:CGRectMake(20 * px, 60 * px, BankWithdrawalView.width * 0.6, 60 * px)];
    CashAmountTitle.textColor = ChoiceBank.textColor;
    CashAmountTitle.text = @"提现金额(收取0.1%手续费)";
//    CashAmount.textAlignment = <#name#>
    CashAmountTitle.font = [UIFont systemFontOfSize:SYRealValue(28 / 2)];
    [BankWithdrawalView addSubview:CashAmountTitle];
    
    
    UILabel * CashAmountLabel = [[UILabel alloc] initWithFrame:CGRectMake(CashAmountTitle.x, CGRectGetMaxY(CashAmountTitle.frame) + 60 * px, 50 * px, 80 * px)];
    CashAmountLabel.textColor = ChoiceBank.textColor;
    CashAmountLabel.text = @"￥";
    //    CashAmount.textAlignment = <#name#>
    CashAmountLabel.font = [UIFont systemFontOfSize:SYRealValue(42 / 2)];
    [BankWithdrawalView addSubview:CashAmountLabel];
    
    
    
    UITextField * CashAmountTextField = [[UITextField alloc] init];
    CashAmountTextField.frame = CGRectMake(CGRectGetMaxX(CashAmountLabel.frame), CashAmountLabel.y, BankWithdrawalView.width - CashAmountLabel.width - CashAmountLabel.x, CashAmountLabel.height);
//    CashAmountTextField.placeholder = @"请输入提现金额";
    [CashAmountTextField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
     [CashAmountTextField setValue:[UIFont systemFontOfSize:SYRealValue(42 / 2)] forKeyPath:@"_placeholderLabel.font"];
    CashAmountTextField.keyboardType = UIKeyboardTypeNumberPad;
    CashAmountTextField.textColor = [UIColor whiteColor];
    [BankWithdrawalView addSubview:CashAmountTextField];
    self.CashAmountTextField = CashAmountTextField;
    
    
    UIView * LinView = [[UIView alloc] initWithFrame:CGRectMake(CashAmountLabel.x, CGRectGetMaxY(CashAmountLabel.frame) + 20 * px, BankWithdrawalView.width - CashAmountLabel.x * 2, 1)];
    LinView.backgroundColor = [UIColor grayColor];
    [BankWithdrawalView addSubview:LinView];
    
    
    
    UILabel * AvailablebalanceLabel = [[UILabel alloc] initWithFrame:CGRectMake(LinView.x, CGRectGetMaxY(LinView.frame) + 40 * px, LinView.width * 0.5, 60 * px)];
    AvailablebalanceLabel.textColor = [UIColor grayColor];
    AvailablebalanceLabel.text = @"可用余额10000元";
    AvailablebalanceLabel.textAlignment = NSTextAlignmentLeft;
    AvailablebalanceLabel.font = [UIFont systemFontOfSize:SYRealValue(26 / 2)];
    [BankWithdrawalView addSubview:AvailablebalanceLabel];
    self.AvailablebalanceLabel = AvailablebalanceLabel;
    
    
    /** 银行选择按钮*/
    UIButton * AllwithdrawalsButton = [[UIButton alloc] initWithFrame:CGRectMake(BankWithdrawalView.width - 200 * px, AvailablebalanceLabel.y, 200 * px, AvailablebalanceLabel.height)];
    [AllwithdrawalsButton setTitle:@"全部提现" forState:UIControlStateNormal];
    [AllwithdrawalsButton setTitleColor:WYCRGBColor(78, 171, 151) forState:UIControlStateNormal];
    AllwithdrawalsButton.titleLabel.font = AvailablebalanceLabel.font;
    [AllwithdrawalsButton  addTarget:self action:@selector(totleMoneyClick) forControlEvents:UIControlEventTouchUpInside];
    [BankWithdrawalView  addSubview:AllwithdrawalsButton];
    
    
    UIButton * loginButton = [[UIButton alloc] initWithFrame:CGRectMake(100 * px, CGRectGetMaxY(BankWithdrawalView.frame) + 150 * px, ScreenWidth - 200 * px, 120 * px)];
    
    
    CAGradientLayer *loginButtonLayer = [CAGradientLayer layer];
    loginButtonLayer.frame = loginButton.bounds;
    loginButtonLayer.cornerRadius = 60 * px;
    loginButtonLayer.startPoint = CGPointMake(0, 0);
    //        gradientLayer
    loginButtonLayer.endPoint = CGPointMake(1, 0);
    loginButtonLayer.locations = @[@(0.2),@(1.0)];//渐变点
    [loginButtonLayer setColors:@[(id)[WYCRGBColor(96, 59, 152) CGColor],(id)[WYCRGBColor(202, 72, 114) CGColor]]];//渐变数组
    [loginButton.layer addSublayer:loginButtonLayer];
    [loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [loginButton setTitle:@"确定" forState:UIControlStateNormal];
    [loginButton addTarget:self action:@selector(loginButtoncClick) forControlEvents:UIControlEventTouchUpInside];
    loginButton.titleLabel.font = [UIFont systemFontOfSize:SYRealValue(30 / 2)];
    
    [self.view addSubview:loginButton];
    
    
}

- (void)ChoiceBankButtonClick{
    
    UIView * view = [[UIView alloc] init];
    view.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
    view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
    [[UIApplication sharedApplication].keyWindow  addSubview:view];
    self.backView = view;
    
    
    UIView * showView = [[UIView alloc] init];
    showView.frame = CGRectMake(0, ScreenHeight / 2, ScreenWidth, ScreenHeight / 2 );
    showView.backgroundColor = backgroundcolor;
    showView.layer.cornerRadius = 20 * px;
    [view addSubview:showView];
    self.showView = showView;
    
    
    /** 标题*/
    UILabel * titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(showView.width / 2 - 100 * px, 20 * px, 200 * px, 60 * px)];
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.text = @"选择银行";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = [UIFont systemFontOfSize:SYRealValue(28 / 2)];
    [showView addSubview:titleLabel];
    
    /**关闭*/
    UIButton * Chosebutton = [[UIButton alloc] initWithFrame:CGRectMake(showView.width - 80 * px, titleLabel.y, 60 * px, 60 * px)];
    [Chosebutton setImage:[UIImage imageNamed:@"pop_dis"] forState:UIControlStateNormal];
    [Chosebutton addTarget:self action:@selector(ChosebuttonClick) forControlEvents:UIControlEventTouchUpInside];
    [showView addSubview:Chosebutton];
    
    
    MSBankCardListController * BankCardListVC = [[MSBankCardListController alloc] init];
    BankCardListVC.view.frame = CGRectMake(0, CGRectGetMaxY(Chosebutton.frame) + 30 * px, showView.width, showView.height - 110 * px);
    BankCardListVC.SettlementButtonDelegate = self;
    [BankCardListVC.addButton addTarget:self action:@selector(addClick) forControlEvents:UIControlEventTouchUpInside];
    self.BankCardListVC = BankCardListVC;
    [showView addSubview:self.BankCardListVC.view];
    
}
- (void)addClick{
    [self ChosebuttonClick];
    MSBuidingBankCardController * buidingVC = [[MSBuidingBankCardController alloc] init];
    [self.navigationController pushViewController:buidingVC animated:NO];

}
- (void)ClickselectIndexRowbanid:(NSString *)banid cardNumber:(NSString *)cardNumber userName:(nonnull NSString *)userName bankName:(NSString *)bankName{
    
     NSString *Interceptstr = [cardNumber substringFromIndex:cardNumber.length - 4];
    
    self.ChoiceBank.text = [NSString stringWithFormat:@"%@(尾号%@)",bankName,Interceptstr];
    
    self.ChoiceBankName.text = [NSString stringWithFormat:@"%@",userName];
    self.banid = banid;
}
- (void)loginButtoncClick{
    
    [MBManager showLoading];
    
    if ([self.ChoiceBank.text isEqualToString:@"请选择银行"]) {
        [MBManager showBriefAlert:@"请选择银行卡"];
        [MBManager hideAlert];
        return;
        
    }
    if (self.CashAmountTextField.text.length == 0) {
        [MBManager showBriefAlert:@"提现金额不能为空"];
        [MBManager hideAlert];
        return;
        
    }
    if ([self.CashAmountTextField.text intValue] > [self.AccountMoney intValue]) {
        [MBManager showBriefAlert:@"提现金额不能大于可用余额"];
        [MBManager hideAlert];
        return;
        
    }
    NSDictionary * params = @{@"uid":[WYCAccountTool unarchiveuid],@"bankcardId":self.banid,@"withdrawMoney":self.CashAmountTextField.text};
    [HTTPRequest POST:MSaddWithdrawRecord params:params succeed:^(id responseObject) {
        if (CodeStr) {
            [MBManager hideAlert];
            [MBManager showBriefAlert:responseObject[@"msg"]];
            [self.navigationController popViewControllerAnimated:NO];
        }else{
            [MBManager hideAlert];
            [MBManager showBriefAlert:responseObject[@"msg"]];
        }
    } failure:^(NSError *error) {
        [MBManager hideAlert];
        [MBManager showBriefAlert:Failed];
    }];
}
- (void)totleMoneyClick{
    
    self.CashAmountTextField.text = [NSString stringWithFormat:@"%@",self.AccountMoney];
}
- (void)CreateNavigationBar{
    
    self.view.backgroundColor = backgroundcolor;
    naviagtionBar = [[CustomNavigationBar alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, [self navBarBottom])];
    [self.view addSubview:naviagtionBar];
    naviagtionBar.titleLabel.text = @"提现";
    naviagtionBar.rightBtn.hidden = YES;
    naviagtionBar.rightBtnTitle.hidden = YES;
    [self.view addSubview:naviagtionBar];
    
    [naviagtionBar.leftBtn setImage:[UIImage imageNamed:@"左箭头"] forState:UIControlStateNormal];
    [naviagtionBar.leftBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    
}
- (void)ChosebuttonClick{
    
    [self.backView removeFromSuperview];
}
- (void)backClick{
    
    [self.navigationController popViewControllerAnimated:NO];
}
@end
